<template>
	<view class="page padding-lr padding-top-sm">
		<view class="box bg-white flex align-center justify-center">
			<view class="left">
				<u-circle-progress active-color="#4CBD66" inactive-color="#E7E7E7" :percent="$wanlshop.bcmul(100,$wanlshop.bcdiv(user.used_storage, user.money.money3).toFixed(2))" width="250">
					<view class="u-progress-content flex flex-column align-center">
						<view class="u-progress-dot margin-bottom-xs text-s" v-if="user.used_storage > 0">已用{{$wanlshop.bcmul(100,$wanlshop.bcdiv(user.used_storage, user.money.money3).toFixed(2))}}%</view>
						<view class="u-progress-dot margin-bottom-xs text-s" v-else>已用0%</view>
						<text class='u-progress-info text-df text-bold'>{{sizeTostr(user.used_storage)}}/{{sizeTostr(user.money.money3)}}</text>
					</view>
				</u-circle-progress>
			</view>
			<view class="right">
				<view class="all">总容量 {{sizeTostr(user.money.money3)}}</view>
				<view class="canuse">可用容量 {{sizeTostr($wanlshop.bcsub(user.money.money3, user.used_storage))}}</view>
			</view>
		</view>
		
		<view class="sku bg-white margin-top-sm">
			<view class="title"> 
				<text>增加内存</text>
				<text @tap="$wanlshop.to('/pages/user/record/card')">购买记录</text>
			</view>
			<view class="list">
				<view class="item flex align-center justify-between" v-for="(item,index) in list" :key="index">
					<view class="left">
						<text>{{item.title}}</text>
						<view class="price">
							<text>￥{{item.price}}元</text>
							<text>{{item.desc}}</text>
						</view>
					</view>
					<button :disabled="loading" class="right" @tap="buy(item)">
						立即购买
					</button>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				loading: false,
				list:[]
			}
		},
		computed: {
			...mapState(['user'])
		},
		onLoad() {
			uni.request({
				url: '/member/card/lists',
				method: 'POST',
				success: res => {
					this.list = res.data;
				}
			});
		},
		methods: {
			async buy(data){
				this.loading = true;
				let datas = await uni.request({url: '/publics/commons/getToken',method: 'POST',data:{type:`card_order_add`}})
				await uni.request({
					url: `/member/card/addOrder`,
					method: 'POST',
					data: {
						card_id: data.id,
						token: datas[1].data
					},
					success: res => {
						this.$wanlshop.to(`/pages/user/money/pay?type=card&id=${res.data.list}`);
						this.loading = false;
					},
					complete: () => {
						setTimeout(() => {
							this.loading = false;
						}, 1500);
					}
				});
			},
			sizeTostr(bytes, decimal = 2) {
				bytes = bytes * 1048576
				var units = ["B", "KB", "MB", "GB", "TB"];
					for (var i = 1; bytes / Math.pow(1024, i) >= 1; i++) {
				}
				return (bytes / Math.pow(1024, i-1)).toFixed(decimal) + units[i-1];
			}
		}
	}
</script>

<style lang="less" scoped>
page {
	background-color: #EDEDED;
}
.box {
	padding: 40rpx 0;
	border-radius: 16rpx;
	.right {
		margin-left: 100rpx;
		.all {
			padding: 0 10rpx;
			height: 68rpx;
			background: #4CBD66;
			border-radius: 10rpx;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 68rpx;
			text-align: center;
			margin-bottom: 24rpx;
		}
		.canuse {
			padding: 0 10rpx;
			height: 68rpx;
			border-radius: 10rpx;
			border: 2rpx solid #4CBD66;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			color: #4CBD66;
			line-height: 68rpx;
			text-align: center;
		}
	}
}
.sku {
	border-radius: 16rpx;
	padding: 34rpx 40rpx;
	.title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 40rpx;
		text {
			&:nth-child(1) {
				height: 48rpx;
				font-size: 34rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				color: #333333;
				line-height: 48rpx;
			}
			&:nth-child(2) {
				color: #65360C;
				font-size: 28rpx;
			}
		}
	}
	.list {
		.item {
			padding: 30rpx 0 28rpx 0;
			border-top: 1rpx solid rgba(182,182,182,0.5);
			// &:last-child {
			// 	border-bottom: none;
			// }
			.right {
				margin: 0;
				padding: 0;
				border: 1px solid transparent;  //自定义边框
				outline: none;    //消除默认点击蓝色边框效果
				width: 196rpx;
				height: 64rpx;
				background: linear-gradient(130deg, #FBE8BD 0%, #FBD0A3 100%);
				border-radius: 32rpx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				color: #65360C;
				line-height: 64rpx;
				text-align: center;
				&::after {
					border: none;
				}
			}
			.left {
				> text {
					height: 44rpx;
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #333333;
					line-height: 44rpx;
				}
				.price {
					margin-top: 2rpx;
					text {
						&:nth-child(2) {
							height: 40rpx;
							font-size: 28rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							color: #969799;
							line-height: 40rpx;
							margin-left: 18rpx;
						}
						&:nth-child(1) {
							height: 40rpx;
							font-size: 28rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: bold;
							color: #EE1B1B;
							line-height: 40rpx;
						}
					}
				}
			}
		}
	}
}
</style>
